package com.jml.基础;

import com.alibaba.fastjson.JSONObject;
import com.jml.基础.entity.UserEntity;
import com.jml.基础.utils.RedisTemplateUtils;
import com.jml.基础.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RedisController {
    
    
    @Autowired
    private RedisUtils redisUtils;
    
    @Autowired
    private RedisTemplateUtils redisTemplateUtils;
    
    @GetMapping("/addUser")
    public String addUser(UserEntity userEntity) {
        // 1.将我们的对象转换成json
        /*String json = JSONObject.toJSONString(userEntity);
        redisUtils.setString("userEntity", json);*/
        // 2.序列化存储底层存的是二进制
        redisTemplateUtils.setObject("userEntity", userEntity);
        return "存储成功~";
    }
    
    @RequestMapping("/getUser")
    public UserEntity getUser(String key) {
        /*String json = redisUtils.getString(key);
        UserEntity userEntity = JSONObject.parseObject(json, UserEntity.class);*/
        return (UserEntity) redisTemplateUtils.getObject(key);
    }
    
}
